package me.aip.mov.response;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.util.Base64;

/**
 * 统一响应格式
 * @author aip9105
 */
@AllArgsConstructor
@Data
@Builder
public class ResponseResult {
    /**
     * 状态码
     */
    private String code;
    /**
     * 状态信息
     */
    private String message;
    /**
     * 响应数据
     */
    private Object data;

    public ResponseResult(ResultCode resultCode, Object data) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = data;
    }

    public static ResponseResult success() {
        return ResponseResult.builder()
                .code(ResultCode.INVOKING_SUCCESS.getCode())
                .message(ResultCode.INVOKING_SUCCESS.getMessage())
                .build();
    }

    public static ResponseResult success(Object data) {
        String msg = null;
        if(data != null){
            msg = Base64.getEncoder().encodeToString(JSON.toJSONString(data).getBytes());

        }
        return ResponseResult.builder()
                .code(ResultCode.INVOKING_SUCCESS.getCode())
                .message(ResultCode.INVOKING_SUCCESS.getMessage())
                .data(msg)
                .build();
    }
    public static ResponseResult successNoEncrypt(Object data) {

        return ResponseResult.builder()
                .code(ResultCode.INVOKING_SUCCESS.getCode())
                .message(ResultCode.INVOKING_SUCCESS.getMessage())
                .data(data)
                .build();
    }
    public static ResponseResult success(String message,Object data) {
        String msg = null;
        if(data != null){
            msg = Base64.getEncoder().encodeToString(JSON.toJSONString(data).getBytes());

        }
        return ResponseResult.builder()
                .code(ResultCode.INVOKING_SUCCESS.getCode())
                .message(message)
                .data(msg)
                .build();
    }
    public static ResponseResult failure(ResultCode resultCode) {
        return ResponseResult.builder()
                .code(resultCode.getCode())
                .message(resultCode.getMessage())
                .build();
    }
    public static ResponseResult failure(String message) {
        return ResponseResult.builder()
                .code(ResultCode.INVOKING_ERROR.getCode())
                .message(message)
                .build();
    }

    public static ResponseResult failure(ResultCode resultCode, Object data) {
        String msg = null;
        if(data != null){
            msg = Base64.getEncoder().encodeToString(JSON.toJSONString(data).getBytes());
        }
        return ResponseResult.builder()
                .code(resultCode.getCode())
                .message(resultCode.getMessage())
                .data(msg)
                .build();
    }
}
